package com.CST.service.impl.ys;

import com.CST.dto.ys.YsAssessmentScoringDTO;
import com.CST.entity.ys.YsManageBudgetData;
import com.CST.mapper.cst.ys.YsAssessmentScoringMapper;
import com.CST.service.ys.YsAssessmentScoringService;
import com.CST.util.ExcelTransfer;
import com.CST.util.PageHelperTool;
import com.CST.vo.ys.YsAssessmentScoringVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 预算考核计分基础表的实现类
 *
 * @author cw
 */
@Service
public class YsAssessmentScoringServiceImpl implements YsAssessmentScoringService {
    @Autowired
    private YsAssessmentScoringMapper baseMapper;

    private final ExcelTransfer<YsAssessmentScoringVO> excelTransfer;

    public YsAssessmentScoringServiceImpl(ExcelTransfer<YsAssessmentScoringVO> excelTransfer) {
        this.excelTransfer = excelTransfer;
    }
    @Override
    public PageInfo<YsAssessmentScoringVO> getScoreList(YsAssessmentScoringDTO dto) {
        dto = setDto(dto);
        List<YsAssessmentScoringVO> resultList =  baseMapper.getScoreList(dto);
        resultList.forEach(s->{
            if (s.getBusinessTotal() == null || s.getBusinessTotal().equals(BigDecimal.ZERO)){
                s.setBusinessRate(getPercent(BigDecimal.ZERO));
            } else{
                s.setBusinessRate(getPercent((s.getBusinessTotal()).divide((s.getTotal() == null || s.getTotal().compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE:s.getTotal()),2, RoundingMode.HALF_UP)));

            }
            if (s.getManageRate() == null || s.getManageRate().equals(BigDecimal.ZERO)){
                s.setManageRate(getPercent(BigDecimal.ZERO));
            }else{
                s.setManageRate(getPercent((s.getManageTotal() == null ?BigDecimal.ZERO:s.getManageTotal()).divide((s.getTotal()== null || s.getTotal().compareTo(BigDecimal.ZERO) == 0  ? BigDecimal.ONE:s.getTotal()),2, RoundingMode.HALF_UP)));
            }
        });
        return PageHelperTool.excutePageInfo(resultList, dto.getPageNum(), dto.getPageSize());
    }


    @Override
    public Boolean importExcel(HttpServletResponse response, YsAssessmentScoringDTO dto) {
        try{
            dto =setDto(dto);
            List<YsAssessmentScoringVO> resultList =  baseMapper.getScoreList(dto);
            excelTransfer.exportExcel(response, resultList, "预算考核计分基础表", "sheet",
                    YsAssessmentScoringVO.class);
        } catch (Exception e){
            throw new RuntimeException("导出失败");
        }
        return true;
    }

    private String getPercent(BigDecimal data) {
        NumberFormat numberFormat = NumberFormat.getPercentInstance();
        return numberFormat.format(data);
    }

    private YsAssessmentScoringDTO setDto(YsAssessmentScoringDTO dto) {
        StringBuilder str = new StringBuilder();
        if (dto.getStartTime() == null || dto.getEndTime() == null){
            throw new RuntimeException("请选择时间");
        }
        dto.setStart(Integer.valueOf(dto.getStartTime().toString().substring(4,6)));
        dto.setEnd(Integer.valueOf(dto.getEndTime().toString().substring(4,6)));
        for (int i = dto.getStart(); i <= dto.getEnd(); i++) {
            str.append("all_").append(i).append("_4+");
        }
        dto.setMonthTotal(str.substring(0,str.length()-1));
        return dto;
    }


}
